<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php $message = ""; ?>
<?php
if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
// Vérifier si un même code existe déjà dans la table conditionvente
if(isset($_POST['Code_tarif'])){
$existe = existeChamp($database_microassur, $microassur, 'tarif', 'Code_tarif', @$_POST['Code_tarif']);
$verif=veriflibelle1("Code_tarif");
}
// Contrôle de saisie des Dates DEBUT/FIN
if(isset($_POST['Date_fin_tarif']) && $_POST['Date_debut_tarif']){
	$datedebut = datevalider($_POST['Date_debut_tarif']);
		$datefin = datevalider($_POST['Date_fin_tarif']);
	$datevalide1 = datevalide($_POST['Date_debut_tarif']);
		$datevalide2 = datevalide($_POST['Date_fin_tarif']);
	$lesdates=verifDate($_POST['Date_debut_tarif'],$_POST['Date_fin_tarif']);
}

if(isset($_POST['Code_tarif']) && strcmp($existe, '0')!=0){
	$message = colorer("Le tarif existe déjà","false");
}elseif(isset($_POST['Code_tarif']) && empty($_POST['Code_tarif'])){
	$message = colorer(" Vous devez saisir le code","false");
}elseif(isset($_POST['Code_tarif']) && !$verif){
	$message = colorer("Le code saisi comporte des caractères spéciaux interdits","false");
}elseif(isset($_POST['Montant_tarif_base'])&& !empty($_POST['Montant_tarif_base']) && !verifNumeric($_POST['Montant_tarif_base'])){
	$message = colorer("Le montant doit être numérique","false");
}elseif(isset($_POST['Taux_tarif_base'])&& !empty($_POST['Taux_tarif_base']) && (!verifDecimal($_POST['Taux_tarif_base']) || !verifTaux(floatval($_POST['Taux_tarif_base']), 2, 3))){
	$message = colorer("Le taux doit être numérique et s’il comporte des décimales doit être sous forme EE.DDD par exemple 5.150","false");
}elseif(isset($_POST['Taux_echeance_mensuel']) && empty($_POST['Taux_echeance_mensuel']) && !verifDecimal($_POST['Taux_echeance_mensuel'])){
	$message = colorer("Vous devez saisir le taux frais échéance mensuel ","false");
}elseif(isset($_POST['Taux_echeance_mensuel']) && !empty($_POST['Taux_echeance_mensuel']) && !verifDecimal($_POST['Taux_echeance_mensuel'])){
	$message = colorer("Le taux frais échéance mensuel doit être numérique et s’il comporte des décimales doit être sous forme EE.DDD par exemple 5.150","false");
}elseif(isset($_POST['Taux_echeance_trimestriel']) && empty($_POST['Taux_echeance_trimestriel']) && !verifDecimal($_POST['Taux_echeance_trimestriel'])){
	$message = colorer("Vous devez saisir le taux frais échéance trimestriel","false");
}elseif(isset($_POST['Taux_echeance_trimestriel']) && !empty($_POST['Taux_echeance_trimestriel']) && !verifDecimal($_POST['Taux_echeance_trimestriel'])){
	$message = colorer("Le taux frais échéance trimestriel doit être numérique et s’il comporte des décimales doit être sous forme EE.DDD par exemple 5.150","false");
}elseif(isset($_POST['Taux_echeance_semestriel']) && empty($_POST['Taux_echeance_semestriel']) && !verifDecimal($_POST['Taux_echeance_semestriel'])){
	$message = colorer("Vous devez saisir le taux frais échéance semestriel","false");
}elseif(isset($_POST['Taux_echeance_semestriel']) && !empty($_POST['Taux_echeance_semestriel']) && !verifDecimal($_POST['Taux_echeance_semestriel'])){
	$message = colorer("Le taux frais échéance semestriel doit être numérique et s’il comporte des décimales doit être sous forme EE.DDD par exemple 5.150","false");
}elseif(isset($_POST['Date_debut_tarif']) && empty($_POST['Date_debut_tarif'])){
	$message = colorer("Vous devez saisir la date de début","false");
}elseif(isset($_POST['Date_debut_tarif']) && !$datedebut){
	$message = colorer("Le format de la date de début est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_debut_tarif']) && !$datevalide1){
	$message = colorer("La date de début est incorrecte ","false");
}elseif(isset($_POST['Date_fin_tarif']) && $_POST['Date_fin_tarif']!=NULL && !$datefin){
	$message = colorer("Le format de la date de fin est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année","false"); 
}elseif(isset($_POST['Date_fin_tarif']) && $_POST['Date_fin_tarif']!=NULL && !$datevalide2){
	$message = colorer("La date de fin est incorrecte ","false");
}elseif(isset($_POST['Date_debut_tarif']) && isset($_POST['Date_fin_tarif']) && !empty($_POST['Date_fin_tarif'])&& !empty($_POST['Date_debut_tarif']) && $lesdates==FALSE){
	$message = colorer("La date de fin doit être postérieure à la date de début","false");
}
else{
	if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
	  $insertSQL = sprintf("INSERT INTO tarif (Identifiant_tarif, Code_tarif, Type_tarif, Montant_tarif_base, Taux_tarif_base, Taux_echeance_mensuel, Taux_echeance_trimestriel, Taux_echeance_semestriel, Date_debut_tarif, Date_fin_tarif, Identifiant_formule, Identifiant_garantie) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
						   GetSQLValueString($_POST['Identifiant_tarif'], "int"),
						   GetSQLValueString($_POST['Code_tarif'], "text"),
						   GetSQLValueString($_POST['Type_tarif'], "text"),
						   GetSQLValueString($_POST['Montant_tarif_base'], "int"),
						   GetSQLValueString($_POST['Taux_tarif_base'], "double"),
						   GetSQLValueString($_POST['Taux_echeance_mensuel'], "double"),
						   GetSQLValueString($_POST['Taux_echeance_trimestriel'], "double"),
						   GetSQLValueString($_POST['Taux_echeance_semestriel'], "double"),
						   GetSQLValueString(dateEN2FR($_POST['Date_debut_tarif']), "date"),
						   GetSQLValueString(dateEN2FR($_POST['Date_fin_tarif']), "date"),
						   GetSQLValueString($_POST['Identifiant_formule'], "int"),
						   GetSQLValueString($_POST['Identifiant_garantie'], "int"));
	
	  mysql_select_db($database_microassur, $microassur);
	   //mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
	  if($Result1){
	  //mysql_query("commit");
		  	$message = colorer("Le tarif est créé","true");
			unset($_POST);
		}
	  else
	  	{$message = colorer("Contacter la DSI, erreur création du tarif","false"); mysql_query("rollback");
		}
	}
}

$maxRows_rs1 = 10;
$pageNum_rs1 = 0;
if (isset($_GET['pageNum_rs1'])) {
  $pageNum_rs1 = $_GET['pageNum_rs1'];
}
$startRow_rs1 = $pageNum_rs1 * $maxRows_rs1;

mysql_select_db($database_microassur, $microassur);
$query_rs1 = "SELECT * FROM tarif";
//$query_limit_rs1 = sprintf("%s LIMIT %d, %d", $query_rs1, $startRow_rs1, $maxRows_rs1);
$query_limit_rs1 = $query_rs1;
$rs1 = mysql_query($query_limit_rs1, $microassur) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);

if (isset($_GET['totalRows_rs1'])) {
  $totalRows_rs1 = $_GET['totalRows_rs1'];
} else {
  $all_rs1 = mysql_query($query_rs1);
  $totalRows_rs1 = mysql_num_rows($all_rs1);
}
$totalPages_rs1 = ceil($totalRows_rs1/$maxRows_rs1)-1;

mysql_select_db($database_microassur, $microassur);
$query_rs_formule = "SELECT * FROM formule";
$rs_formule = mysql_query($query_rs_formule, $microassur) or die(mysql_error());
$row_rs_formule = mysql_fetch_assoc($rs_formule);
$totalRows_rs_formule = mysql_num_rows($rs_formule);

mysql_select_db($database_microassur, $microassur);
$query_rs_garantie = "SELECT * FROM garantie";
$rs_garantie = mysql_query($query_rs_garantie, $microassur) or die(mysql_error());
$row_rs_garantie = mysql_fetch_assoc($rs_garantie);
$totalRows_rs_garantie = mysql_num_rows($rs_garantie);

$queryString_rs1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rs1") == false && stristr($param, "totalRows_rs1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rs1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rs1 = sprintf("&totalRows_rs1=%d%s", $totalRows_rs1, $queryString_rs1);
/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="T0017"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../condvente.css.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../form_ie7.css" /><![endif]-->
            <link type="text/css" href="../../../css/custom-theme/jquery-ui-1.8.11.custom.css" rel="stylesheet" />	
            <script type="text/javascript" src="../../../js/jquery-1.5.1.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery-ui-1.8.11.custom.min.js"></script>
            <script type="text/javascript" src="../../../js/jquery.ui.datepicker-fr.js"></script>
            <script type="text/javascript" src="../../../js/scripts.js"></script>
	</head>
<body id="tarif">
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Tarif";
					$nommodule2="Liste tarifs";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="cor">
			<!--/***************************************/-->
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
					<table class="tab_list">
						  <tr id="niv1">
							<td class="t1">CODE</td>
							<td class="t2">MONTANT</td>
							<td class="date">DATE DEBUT/FIN</td>
							<td class="t3">TYPE</td>
							<td class="t4">TAUX</td>
							<td rowspan="2" class="tg4"><div>%&nbsp;Frais</div> Mens.&nbsp;&nbsp;&nbsp; Trim.&nbsp;&nbsp;&nbsp; Sem.</td>
							<td class="t5">GARANTIE</td>
							<td>FORMULE</td>
						  </tr>
					</table>
				<div class="content_table">
					<table class="tab_list ns0">
						  <?php $i=1; do {
							mysql_select_db($database_microassur, $microassur);
							$query_rs_lab_formule = "SELECT * FROM formule WHERE Identifiant_formule =  '". $row_rs1['Identifiant_formule']."'";
							$rs_lab_formule = mysql_query($query_rs_lab_formule, $microassur) or die(mysql_error());
							$row_rs_lab_formule = mysql_fetch_assoc($rs_lab_formule);
							$totalRows_rs_lab_formule = mysql_num_rows($rs_lab_formule);
							
							mysql_select_db($database_microassur, $microassur);
							$query_rs_lab_garantie = "SELECT * FROM garantie WHERE Identifiant_garantie = '". $row_rs1['Identifiant_garantie']."'";
							$rs_lab_garantie = mysql_query($query_rs_lab_garantie, $microassur) or die(mysql_error());
							$row_rs_lab_garantie = mysql_fetch_assoc($rs_lab_garantie);
							$totalRows_rs_lab_garantie = mysql_num_rows($rs_lab_garantie); 
						  ?>
						  <?php if ($totalRows_rs1 > 0) { // Show if recordset not empty ?>
							<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
							  <td class="t1"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo $row_rs1['Code_tarif']; ?></a></td>
							  <td class="t2"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo $row_rs1['Montant_tarif_base']; ?></a></td>
							  <td class="date"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo dateEN2FR($row_rs1['Date_debut_tarif']) . "/" ?><?php if($row_rs1['Date_fin_tarif']!=NULL) echo dateEN2FR($row_rs1['Date_fin_tarif']); else echo $indefini; ?></a></td>
							  <td class="t3"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo $row_rs1['Type_tarif']; ?></a></td>
							  <td class="t4"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo $row_rs1['Taux_tarif_base']; ?></a></td>
							  <td class="t4"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo $row_rs1['Taux_echeance_mensuel']; ?></a></td>
							  <td class="t4"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo $row_rs1['Taux_echeance_trimestriel']; ?></a></td>
							  <td class="t4"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo $row_rs1['Taux_echeance_semestriel']; ?></a></td>
							  <td class="t5"><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo tronque($row_rs_lab_garantie['Code_garantie'], 13); ?></a></td>
							  <td><a href="modtarif.php?Identifiant_tarif=<?php echo $row_rs1['Identifiant_tarif']; ?>"><?php echo tronque($row_rs_lab_formule['Code_formule'], 13); ?></a></td>
							</tr>
							<?php } // Show if recordset not empty ?>

						  <?php $i++; } while ($row_rs1 = mysql_fetch_assoc($rs1)); ?>
					</table>
				</div>
			</div>
		<div class="imgright"></div>
			<div class="cleared"></div>
		<!--/***************************************/-->
			<div id="br"></div>
			
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
			<div class="imgleft2"></div>
				<div class="imgmiddle2">
					<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
					<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
						<div class="cleared"></div>
						<div class="content_tableform1">
						  <table>
							<tr>
							  <td>CODE</td>
							</tr>
								<tr>
								  <td><input class="resize_1" type="text" name="Code_tarif" value="<?php echo remind_text('Code_tarif'); ?>" size="32" maxlength="5" /></td>
								</tr>
								<tr>
									<td>MONTANT <input type="text" name="Montant_tarif_base" value="<?php echo remind_text('Montant_tarif_base'); ?>" size="10" maxlength="10" /></td>
									<td>TAUX <input type="text" name="Taux_tarif_base" value="<?php echo remind_text('Taux_tarif_base'); ?>" size="10" maxlength="6" /></td>
									  <td>TYPE <select name="Type_tarif">
									  <?php do{ ?>
												<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php echo remind_select('Type_tarif', $row_rs_dico2['Code_element']) ?>><?php echo $row_rs_dico2['Nom_element']; ?></option>
									  <?php } while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
									  </select>
									  </td>
								</tr>
							<tr><td colspan="3" class="tx">TAUX FRAIS ECHEANCE</td></tr>
								<tr>
									<td>MENSUEL <input type="text" name="Taux_echeance_mensuel" value="<?php echo remind_text('Taux_echeance_mensuel'); ?>" size="10" maxlength="10" /></td>
									<td>TRIMESTRIEL <input type="text" name="Taux_echeance_trimestriel" value="<?php echo remind_text('Taux_echeance_trimestriel'); ?>" size="10" maxlength="10" /></td>
									<td><div>SEMESTRIEL</div> <input type="text" name="Taux_echeance_semestriel" value="<?php echo remind_text('Taux_echeance_semestriel'); ?>" size="10" maxlength="10" /></td>
								</tr>
								<tr>
								  <td>DATE DEBUT <input type="text" name="Date_debut_tarif" class="date_pick" value="<?php echo remind_text('Date_debut_tarif'); ?>" size="10" maxlength="10" /></td>
								  <td>DATE FIN <input type="text" name="Date_fin_tarif" class="date_pick" value="<?php echo remind_text('Date_fin_tarif'); ?>" size="10" maxlength="10" /></td>
								</tr>
								</table>
								<table class="tab2">
								<tr>
								  <td>FORMULE <select name="Identifiant_formule" class="select">
									<?php 
							do {  
							?>
									<option value="<?php echo $row_rs_formule['Identifiant_formule']?>" <?php echo remind_select('Identifiant_formule', $row_rs_formule['Identifiant_formule']) ?> ><?php echo $row_rs_formule['Libelle_formule']?></option>
									<?php
							} while ($row_rs_formule = mysql_fetch_assoc($rs_formule));
							?>
								  </select>
								  </td>
								  <td>GARANTIE <select name="Identifiant_garantie">
								  <option value="">Sélectionner une garantie</option>
									<?php 
							do {  
							?>
									<option value="<?php echo $row_rs_garantie['Identifiant_garantie']?>" <?php echo remind_select('Identifiant_garantie', $row_rs_garantie['Identifiant_garantie']) ?> ><?php echo $row_rs_garantie['Libelle_garantie']?></option>
									<?php
							} while ($row_rs_garantie = mysql_fetch_assoc($rs_garantie));
							?>
								  </select>
								  </td>
								</tr>
							  </table>
							</div>
				</div>
					<div class="imgright2"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
						<div class="cleared"></div>
  <input type="hidden" name="Identifiant_tarif" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs1);
mysql_free_result($rs_formule);
mysql_free_result($rs_garantie);
mysql_free_result($rs_lab_formule);
mysql_free_result($rs_lab_garantie);
?>
